﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Castle.Services.Transaction;
using Meqs.Entity;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    public class AppraisalsLogic : CommonLogic<Appraisals, int>, IAppraisalsLogic
    {
        #region Implementation of ICommonLogic<Appraisals, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(Appraisals entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(Appraisals entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int Delete(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("Appraisals.Delete", ht);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("Appraisals.Delete", ht);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public Appraisals FindById(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<Appraisals>("Appraisals.FindById", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int FindCount(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByCount("Appraisals.FindCount", ht);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<Appraisals> FindAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<Appraisals>("Appraisals.Find", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<Appraisals> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion


        [Transaction(TransactionMode.Requires)]
        public int SaveAppraisals(IList<Appraisals> results, int eiid, String schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("ElID", eiid);
            IList<Appraisals> apprList = FindAll(ht);
            IList<Appraisals> delAppr = new List<Appraisals>();
            foreach (Appraisals appraisals in apprList)
            {
                foreach (Appraisals appraisals1 in results)
                {
                    if (appraisals1.RID == appraisals.RID)
                    {
                        delAppr.Add(appraisals);
                        break;
                    }
                }
            }
            foreach (Appraisals appraisals in delAppr)
            {
                apprList.Remove(appraisals);
            }
            foreach (Appraisals appraisals in apprList)
            {
                Delete(appraisals.RID, schoolMark);
            }

            foreach (Appraisals appr in results)
            { 
                appr.ElID = eiid;
                ht = new Hashtable();
                ht.Add("Appraisals", appr);
                ht.Add("SchoolMark", schoolMark);
                if (appr.RID == 0)
                    InsertCustomer("Appraisals.InsertAppraisalsEx", ht);
                else
                    UpdateCustomer("Appraisals.UpdateAppraisalsEx", ht);
            }
            return 0;
        }

        /// <summary>
        /// 删除评估项相关的评价
        /// </summary>
        [Transaction(TransactionMode.Requires)]
        public int DeleteApprByEIID(int eiid, String schoolMark)
        { 
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("EIID", eiid);
            return DeleteCustomer("Appraisals.DeleteApprByEIID", ht);
        }

        public IList<String> GetApprForScore(int eiid, String schoolMark, double score)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("EIID", eiid);
            ht.Add("SCORE", score);
            return FindCustomer<String>("Appraisals.GetApprForScore", ht);
        }
    }
}
